#!/usr/bin/python
import sys
import os
import struct
import time

packet_head = [
	0x11,
	0x22,
	0x33,
	0x44,
	0x55,
	0x66,
	
	0xeb,
	0x90,
	0xeb,
	0x90,
	0xeb,
	0x90,
	
        0x99,
        0x9F,
	0x01,
	0x00,
	0x00,
        0x00,
	0x00,
	0x00,
	0x00,
]

crc_table = [0 for x in range(256)]
def init_crc_table():
    for i in range(256):
        c = i
        for j in range(8):
            if (c & 1):
                c = 0xedb88320 ^ (c >> 1)
            else:
                c = c >> 1
            crc_table[i] = c
    return crc_table

def calculate_crc32(para_crc, payload, size):
    tmp_crc = para_crc
    for i in range(size):
        tmp_crc = crc_table[(tmp_crc ^ payload[i]) & 0xff] ^ (tmp_crc >> 8)
    return tmp_crc


def packet_fun(payload, len):
    packet_head[17] = len + 2
    packet = packet_head+payload
    crc_table = init_crc_table()
    crc = 0xFFFFFFFF
    crc = calculate_crc32(crc, payload, len)
    packet.append(crc&0xFF)
    packet.append((crc>>8)&0xFF)
    packet.append((crc>>16)&0xFF)
    packet.append((crc>>24)&0xFF)
    #print(crc_table)
    #print(crc)
    #print(packet)
    return packet

def unpacket_fun(packet):
    payload = packet[21:-4]
    #print(payload)
    return payload